package com.test.suanfa;

import java.util.HashMap;
import java.util.Map;

public class 两数之和 {

    public static void main(String[] args) {
        int[] nums = {5, 2, 3, 4, 5, 6};
        test(nums, 11);
        test2(nums, 11);
    }

    public static void test2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer v = map.get(target - nums[i]);
            if (v != null) {
                System.out.println(nums[i]+","+v);
                break;
            }
            map.put(nums[i], nums[i]);
        }
    }

    public static void test1(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer v = map.get(target - nums[i]);
            if (v != null) {
                System.out.println(i + "," + v);
                break;
            }
            map.put(nums[i], i);
        }
    }

    public static void test(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer v = map.get(target - nums[i]);
            if (v != null) {
                System.out.println(i + "," + v);
                break;
            }
            map.put(nums[i], i);
        }
    }

}
